package se.jocke.nb.log.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.geom.Rectangle2D;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextPane;

/**
 *
 * @author jocke
 */
public class LineNumberListCellRenderer extends DefaultListCellRenderer {

    private final JTextPane textPane;

    public LineNumberListCellRenderer(JTextPane textPane) {
        this.textPane = textPane;
    }

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (value != null) {

            LineNumber lineNumber = (LineNumber) value;
            final String text = "" + lineNumber.getNumber();

            label.setText(text);

            if (!isSelected && lineNumber.getBgColor() != null) {
                label.setBackground(lineNumber.getBgColor());
            }

            if (lineNumber.getFgColor() != null) {
                label.setForeground(lineNumber.getFgColor());
            }

            if (lineNumber.getFont() != null) {
                label.setFont(lineNumber.getFont());
                FontMetrics fontMetrics = textPane.getFontMetrics(lineNumber.getFont());
                label.setPreferredSize(new Dimension(30, fontMetrics.getHeight()));
            }

        }
        return label;
    }
}
